<template>
  <el-card style="margin-bottom: 10px">
    <el-form :inline="true" :disabled="cc.isShow" lass="demo-form-inline my-category">
      <el-form-item label="一级分类">
        <el-select v-model="category1Id" placeholder="请选择">
          <el-option
            v-for="c1 in categoryStore.category1List"
            :key="c1.id"
            :label="c1.name"
            :value="c1.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select v-model="category2Id" placeholder="请选择">
          <el-option
            v-for="c2 in categoryStore.category2List"
            :key="c2.id"
            :label="c2.name"
            :value="c2.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select v-model="category3Id" placeholder="请选择">
          <el-option
            v-for="c3 in categoryStore.category3List"
            :key="c3.id"
            :label="c3.name"
            :value="c3.id"
          />
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script lang="ts">
export default {
  name: "CategorySelector",
};
</script>

<script lang="ts" setup>
import { reactive, onMounted, computed } from "vue";
import { useCategoryStore } from "@/stores/categoryList";
import { ElMessage } from "element-plus";
const categoryStore = useCategoryStore();
//接收父组件传来的参数
const cc = defineProps(["isShow"]);

// 点击提交按钮触发的事件函数
const onSubmit = () => {
  console.log("submit!");
};

// 计算属性 computed????
const category1Id = computed({
  get() {
    // console.log("---------");
    // console.log(categoryStore.category1Id);
    return categoryStore.category1Id as number;
  },
  set(val: number) {
    // 当一级导航id改变就发送请求回去二级导航数据 va就是一级导航的id
    categoryStore.getCategory2ListData(val);
    // console.log("检测属性启动");
  },
});

// 当二级导航id改变发送请求获取三级导航数据
const category2Id = computed({
  get() {
    return categoryStore.category2Id as number;
  },
  set(val: number) {
    // 当一级导航id改变就发送请求回去二级导航数据 va就是一级导航的id
    categoryStore.getCategory3ListData(val);
  },
});

// 三级id
const category3Id = computed({
  get() {
    return categoryStore.category3Id as number;
  },
  set(val: number) {
    categoryStore.saveCategory3Id(val);
  },
});

onMounted(async () => {
  // 当组件挂载时获取一级导航参数
  await categoryStore.getCategory1ListData();
  ElMessage.success("获取三级导航列表一级导航成功");
});
</script>

<style></style>
